package ua.keeper.query;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class QueryActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // get DataBase
        QueryApp appState = ((QueryApp)getApplicationContext());
        QuerySettings sett = new QuerySettings(appState.getDataBase());
        // set TP name
        TextView tpView = (TextView) findViewById(R.id.tpView);
        tpView.setText(sett.getTPName());
    }
    
    /*****************************************************************************************
    /** Обработка клика по кнопке "Заявка" */
    public void onRequestButtonClicked(View v)
    {
    	Intent myIntent = new Intent(QueryActivity.this, WeekListActivity.class);
    	QueryActivity.this.startActivity(myIntent);
    }
    
    /*****************************************************************************************
    /** Обработка клика по кнопке "Настройки" */
    public void onSettingsButtonClicked(View v)
    {
    	/**setContentView(R.layout.settings);
    	Intent myIntent = new Intent(QueryActivity.this, SettingsActivity.class);
    	QueryActivity.this.startActivity(myIntent);*/
    	Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "ONE_D_MODE");
        intent.putExtra("PROMPT_MESSAGE", "Наведите камеру на билет...");
        intent.putExtra("RESULT_DISPLAY_DURATION_MS", 0L);
        startActivityForResult(intent, 0);
    }
    
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                TextView tpView = (TextView) findViewById(R.id.tpView);
                tpView.setText(contents);
                // Handle successful scan
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            	TextView tpView = (TextView) findViewById(R.id.tpView);
                tpView.setText("C A N C L E D");
            }
        }
    }
    /*****************************************************************************************
    /** Обработка клика по кнопке "Выход" */
    public void onExitButtonClicked(View v)
    {
    	AlertDialog deleteAlert = new AlertDialog.Builder(this).create();
    	deleteAlert.setTitle("Выход");
    	deleteAlert.setMessage("Вы уверены, что хотите выйти из программы?");
    	deleteAlert.setButton("Да", new DialogInterface.OnClickListener()
    		{
		    	/**@Override*/
		    	public void onClick(DialogInterface dialog, int which)
		    	{               
		        	finish();
		    	}
    		}
    	);
    	deleteAlert.setButton2("Нет", new DialogInterface.OnClickListener()
			{
		    	/**@Override*/
		    	public void onClick(DialogInterface dialog, int which)
		    	{               
		    	}
			}
    	);
    	deleteAlert.show();
    }
}